﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for Recorder.xaml
    /// </summary>
    public partial class RecorderControl : UserControl
    {
        Recorder jane;
        Burner max;
        Brush buttonBk;

        delegate void VoidInvokeHandler();

        public RecorderControl(Recorder j, Burner m)
        {
            InitializeComponent();

            max = m;
            jane = j;

            buttonBk = PauseButton.Background;

            SongBookLabel.Content = jane.MusicSongbookName;

            jane.MusicPlaying += new Recorder.VoidEventHandler(MusicPlaying);
            jane.MusicStopping += new Recorder.VoidEventHandler(MusicStopping);
            jane.MusicSkipping += new Recorder.VoidEventHandler(MusicSkipping);
            jane.MusicStopped += new Recorder.VoidEventHandler(MusicStopped);

            jane.SongUnloaded += new Recorder.VoidEventHandler(SongUnloaded);
            jane.SongStopped += new Recorder.VoidEventHandler(SongStopped);
            jane.SongStarted += new Recorder.VoidEventHandler(SongStarted);
            jane.SongLoaded += new Recorder.VoidEventHandler(SongLoaded);
            jane.SongLoading += new Recorder.VoidEventHandler(jane_SongLoading);

            jane.RecorderStarted += new Recorder.VoidEventHandler(RecorderStarted);
            jane.RecorderStarting += new Recorder.VoidEventHandler(RecorderStarting);
            jane.RecorderTracked += new Recorder.VoidEventHandler(RecorderTracked);
            jane.RecorderUnpaused += new Recorder.VoidEventHandler(RecorderUnpaused);
            jane.RecorderPaused += new Recorder.VoidEventHandler(RecorderPaused);
            jane.RecorderFinished += new Recorder.VoidEventHandler(RecorderFinished);
            jane.RecorderFinishing += new Recorder.VoidEventHandler(jane_RecorderFinishing);
            jane.RecorderCDLengthExceeded += new Recorder.VoidEventHandler(jane_RecorderCDLengthExceeded);

        }

        void jane_RecorderCDLengthExceeded()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (VoidInvokeHandler)delegate()
            {
            SSMessageBox.ShowNonWait("Warning", "Sound Servant has automatically inserted a Discmark because the current disc has exceeded 74 minutes in length.\r\n\r\nHave you correctly confugured Sound Servant to automatically Discmark your recordings? If you see this message regularly, please consult your sound servant.", SSMessageBoxType.ExclamationOk);
            });
        }

        void jane_RecorderFinishing()
        {

        }

        void RecorderFinished()
        {
            SS.TenthOfASecondTimer.Tick -= RecorderTicker;
            MusicStatus.Content = "Ready (" + jane.MusicPlayListCount + " songs)";
            RecorderStatus.Content = "Finished";
            StatusLabel.Content = "Finished";
            StartRecordingButton.IsEnabled = false;
            StopRecordingButton.IsEnabled = false;
            PauseButton.IsEnabled = false;
            DiscMarkButton.IsEnabled = false;
            VolumeMeter.Stop();
        }

        void RecorderPaused()
        {
            PauseButton.Background = Brushes.Red;
            PauseButtonLabel.Foreground = Brushes.White;
            PauseButtonSecondaryLabel.Foreground = Brushes.White;
            RecorderTimer.Foreground = Brushes.Red;
            PauseButtonLabel.Content = "Unpause";
            StatusLabel.Content = "Paused";
            SecondaryStatusLabel.Content = "To remsume the recording, click [Unpause]";
        }

        void RecorderUnpaused()
        {
            PauseButton.Background = buttonBk;
            PauseButtonLabel.Foreground = Brushes.Black;
            PauseButtonSecondaryLabel.Foreground = Brushes.Black;
            RecorderTimer.Foreground = Brushes.Black;
            PauseButtonLabel.Content = "Pause";
            StatusLabel.Content = "Active : Disc : " + jane.RecorderCurrentDiscNumber + " | Track " + jane.RecorderCurrentTrackNumber;
            SecondaryStatusLabel.Content = "At the begining of each talk, click [New Track]";
        }

        void RecorderTracked()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (VoidInvokeHandler)delegate()
            {
                StatusLabel.Content = "Active : Disc : " + jane.RecorderCurrentDiscNumber + " | Track " + jane.RecorderCurrentTrackNumber;
                SecondaryStatusLabel.Content = "At the begining of each talk, click [New Track]";
            });
        }

        void RecorderStarting()
        {
            RecorderStatus.Content = "Starting...";
            StatusLabel.Content = "Starting";
        }

        void RecorderStarted()
        {
            SS.TenthOfASecondTimer.Tick += new EventHandler(RecorderTicker);
            RecordingHolder.Content = new RecordingControl(jane.Recording);
            MusicStatus.Content = "Disabled While Recording";
            RecorderStatus.Content = "Recording";
            StatusLabel.Content = "Active";
            WelcomeMessage.Visibility = Visibility.Collapsed;
            CenterContent.Visibility = Visibility.Visible;
            VolumeMeter.Start();

            StartRecordingButtonLabel.Content = "New Track";
            StartRecordingButtonSecondaryLabel.Content = "insert a track mark into the recording";
        }

        void RecorderTicker(object sender, EventArgs e)
        {
            RecorderTimer.Content = SS.ParseTimeSpan(jane.RecorderTimePassed);
        }



        void SongLoaded()
        {
            SongTimer.Content = jane.SongTimeLeft;
            SongInfo1.Content = jane.SongLoadedTitle;
            SongInfo2.Content = jane.SongLoadedScripture;
        }
        void SongStarted()
        {
            SongNumber.IsReadOnly = true;
            Keypad.IsEnabled = false;
            PlaySongButtonLabel.Content = "Stop";
            SS.TenthOfASecondTimer.Tick += new EventHandler(SongTicker);
        }
        void SongTicker(object sender, EventArgs e)
        { SongTimer.Content = jane.SongTimeLeft; }
        void SongStopped()
        {
            SongNumber.IsReadOnly = false;
            Keypad.IsEnabled = true;
            PlaySongButtonLabel.Content = "Play";
            SS.TenthOfASecondTimer.Tick -= SongTicker;
        }
        void SongUnloaded()
        {
            SongTimer.Content = "-0:00";
            SongInfo1.Content = "No Song Loaded";
            SongInfo2.Content = "enter a song number";
            SongNumber.Text = "";
        }
        private void SongNumber_TextChanged(object sender, TextChangedEventArgs e)
        { jane.SongToLoad = SongNumber.Text; }
        private void PlaySongButton_Click(object sender, RoutedEventArgs e)
        { jane.SongStartStop(); }
        void jane_SongLoading()
        {
            SongInfo1.Content = "Loading...";
        }
        private void SongNumPad0_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("0"); }
        private void SongNumPad1_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("1"); }
        private void SongNumPad2_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("2"); }
        private void SongNumPad3_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("3"); }
        private void SongNumPad4_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("4"); }
        private void SongNumPad5_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("5"); }
        private void SongNumPad6_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("6"); }
        private void SongNumPad7_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("7"); }
        private void SongNumPad8_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("8"); }
        private void SongNumPad9_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) SongNumber.AppendText("9"); }
        private void SongNumPadClear_Click(object sender, MouseButtonEventArgs e) { if (jane.SongState != SongStates.Playing) { SongNumber.Text = ""; jane.SongUnload(); } }



        void MusicSkipping()
        { MusicStatus.Content = "Skipping Current Song"; }
        void MusicStopping()
        { MusicStatus.Content = "Stopping"; }
        void MusicPlaying()
        {
            PlaySkipMusicButtonLabel.Content = "Skip";
            PlaySkipMusicButtonSecondaryLabel.Content = "the current song";
            MusicStatus.Content = "Playing (" + jane.MusicCurrentlyPlaying + ")";
        }
        void MusicStopped()
        {
            PlaySkipMusicButtonLabel.Content = "Play";
            PlaySkipMusicButtonSecondaryLabel.Content = "welcome music";
            MusicStatus.Content = "Ready (" + jane.MusicPlayListCount + " songs)";
        }
        private void PlayMusicButton_Click(object sender, RoutedEventArgs e)
        { jane.MusicPlaySkip(); }
        private void StopMusicButton_Click(object sender, RoutedEventArgs e)
        { jane.MusicStop(); }




        private void StartRecordingButton_Click(object sender, RoutedEventArgs e)
        {
            if (jane.RecorderState == RecorderStates.Recording)
                jane.RecorderTrack();
            else
                jane.StartRecording();
        }

        private void StopRecordingButton_Click(object sender, RoutedEventArgs e)
        {
            jane.RecorderStop();
        }

        private void DiscMarkButton_Click(object sender, RoutedEventArgs e)
        {
            if (SSMessageBox.Show("Create a New Disc?", "Are you sure you want to create a new Disc? If correctly configured, Sound Servant will automatically create Disc Marks for you when recording regular meetings. If you are using the Disc Mark feature on a regular basis, please inform your Sound Servant.", SSMessageBoxType.ExcalamationYesNo) == SSMessageBoxResult.Yes)
                jane.RecorderDisc();
        }

        private void PauseButton_Click(object sender, RoutedEventArgs e)
        {
            jane.RecorderPauseUnpause();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void BurnButton_Click(object sender, RoutedEventArgs e)
        {
            if (jane.Recording != null)
            {
                AddCDWindow addCDWindow = new AddCDWindow(jane.Recording, max);
                addCDWindow.ShowDialog();
            }
        }

        private void CopyMP3Button_Click(object sender, RoutedEventArgs e)
        {
            if (jane.Recording != null && !jane.Recording.Current)
            {
                CopyMP3Window copyMP3Window = new CopyMP3Window(jane.Recording);
                copyMP3Window.ShowDialog();
            }
        }
    }
}
